package com.wuzheng.wisgo.sanitation.cloud.paraandgetutil;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class HttpClientUtil {

    public static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    /*
     * @MethodName: getString
     * @Description: get请求连接
     * @Author: sunpeng
     * @Date: 2019/11/15
    **/
    public static String getString(final String url) throws Exception {
        //将URL与参数拼接
        HttpGet getMethod = new HttpGet(url);
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response;
        String responseStr;
        response = httpClient.execute(getMethod); //发起GET请求
        responseStr = EntityUtils.toString(response.getEntity(), "UTF-8");
        return responseStr;
    }

    /**
     * 向指定URL发送GET方法的请求
     *
     * @param url 发送请求的URL
     * @return URL 所代表远程资源的响应结果
     */
    public static String sendGet(String url) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            return null;
        } finally { // 使用finally块来关闭输入流
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
    /*
     * @MethodName: postString
     * @Description: post求连接
     * @Author: sunpeng
     * @Date: 2019/11/15
     **/
    public static String postString(final String url, final String requestStr) throws Exception {
        logger.info("url:\n" + url);
        System.out.println("["+Thread.currentThread().hashCode()+"]-url:\n" + url);
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postMethod = new HttpPost(url);
        HttpResponse response;
        String responseStr = null;
        try {
            postMethod.setEntity(new StringEntity(requestStr, "utf-8"));
            System.out.println("["+Thread.currentThread().hashCode()+"]-requestStr:\n" + requestStr);
            logger.info("requestStr:\n" + requestStr);
            response = httpClient.execute(postMethod); //发起POST请求
            responseStr = EntityUtils.toString(response.getEntity(), "UTF-8");
            System.out.println("["+Thread.currentThread().hashCode()+"]-responseStr:\n" + responseStr);
            logger.info("responseStr:\n" + responseStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return responseStr;
    }

    public static JSONObject getJsonObject(final String url) throws Exception {
        return JSONObject.parseObject(getString(url));
    }

    public static JSONArray getJsonArray(final String url) throws Exception {
        return JSONArray.parseArray(getString(url));
    }

    public static JSONObject postJsonObject(final String url, final String requestStr) throws Exception {
        return JSONObject.parseObject(postString(url, requestStr));
    }

    public static JSONArray postJsonArray(final String url, final String requestStr) throws Exception {
        return JSONArray.parseArray(postString(url, requestStr));
    }
}
